<template>
  <div>

    <van-nav-bar title="商品详情" right-text="按钮" left-arrow @click-left="$router.back()">
      <template #right>
        <van-icon name="ellipsis" />
      </template>
    </van-nav-bar>
    <div class="product-info">
      <img style="width:100%" :src="info.goodsCoverImg" alt="">
      <div class="info-goodsName">
        {{ info.goodsName }}
      </div>
      <p class="info-p">免邮费，顺丰快递</p>
      <p class="info-price">
        ￥{{ info.sellingPrice }}
      </p>
      <ul class="info-ul">
        <li>概述</li>
        <li>参数</li>
        <li>安装服务</li>
        <li class="info-ul-li">常见问题</li>
      </ul>
      <div v-html="info.goodsDetailContent" class="info-content"></div>
    </div>
    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" color="#ee0a24" />
      <van-action-bar-icon icon="cart-o" text="购物车" :badge="store.cartNum" @click="$router.push('/cart')" />
      <van-action-bar-icon icon="star" text="已收藏" color="#ff5000" />
      <van-action-bar-button type="warning" text="加入购物车" @click="add" />
      <van-action-bar-button type="danger" text="立即购买" />
    </van-action-bar>

  </div>
</template>

<script setup>
import { onMounted, reactive } from 'vue';
import { getInfoData,addCart } from "../../api/index.js";
import { useRoute } from 'vue-router';
import { useCart } from '../../store/cartStore.js'
import { showFailToast,showSuccessToast } from 'vant';

let route = useRoute();
let info = reactive({})
let store = useCart();

onMounted(() => {
  getData()
})


store.getCartListAsync()

let getData = async () => {
  console.log(1111);
  let { data } = await getInfoData(route.params.productId)
  for (const key in data) {
    info[key] = data[key]
  }
}

let add = async () => {
  let { resultCode, message } = await addCart({
    goodsId: route.params.productId,
    goodsCount: 1
  });
  if (resultCode === 200) {
    showSuccessToast("添加成功");
    //获取购物车数据
    store.getCartListAsync()
  } else {
    showFailToast(message)
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  position: fixed;
  width: 100%;
  height: 1.17333rem;
  top: 0;
  left: 0;
  z-index: 100;
  line-height: 1.17333rem;
  padding: 0 0.26667rem;
  box-sizing: border-box;
  background: #fff;
  border-bottom: 0.02667rem solid #dcdcdc;
}

.product-info {
  margin-top: 1.17333rem;
  padding: 0 0.26667rem;

  .info-goodsName {
    font-size: 0.48rem;
    text-align: left;
    color: #333;
  }

  .info-p {
    font-size: 0.37333rem;
    text-align: left;
    color: #999;
    padding: 0.13333rem 0;
  }

  .info-price {
    color: #f63515;
    font-size: 0.58667rem;
    text-align: left;
  }

  .info-ul {
    width: 100%;
    height: 30px;
    margin: 26px 0;
    display: flex;
    justify-content: space-between;

    li {
      flex: 1;
      padding: 0.13333rem 0;
      text-align: center;
      font-size: 0.4rem;
      border-right: 0.02667rem solid #999;
      box-sizing: border-box;
    }
  }

  .info-ul .info-ul-li {
    border-right: none;
  }

  .info-content {
    padding: 0 0.53333rem;
  }
}
</style>